package com.dm.shop.service.impl;

import com.dm.shop.service.QnUploadSerive;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @desc:
 * @date: 2021/7/24 10:54
 */
@Service
public class QnUploadServiceImpl implements QnUploadSerive, InitializingBean {
    @Resource
    private UploadManager uploadManager;
    
    @Resource
    private BucketManager bucketManager;

    @Resource
    private Auth auth;

    @Value("${qiniu.Bucket}")
    private String bucket;

    @Value("${qiniu.cdn.prefix}")
    private String prefix;

    /**
     * 定义七流云上传的相关策略
     */
    private StringMap putPolicy;


    /**
     * 以文件的形式上传
     * @param file
     * @return
     * @throws QiniuException
     */
    @Override
    public String uploadFile(File file,String fileName) throws QiniuException {
        Response response = this.uploadManager.put(file, fileName, getUploadToken());
        int retry = 0; //重审
        while (response.needRetry() && retry < 3)
        {
            response = this.uploadManager.put(file,fileName,getUploadToken());
            retry++;
        }
        if(response.statusCode == 200)
        {
            //返回 图片连接
            return new StringBuffer().append(prefix).append(fileName).toString();
        }
        return "上传失败！";
    }

    /**
     * 以流的形式上传
     * @param inputStream
     * @param fileName
     * @return
     * @throws QiniuException
     */
    @Override
    public String uploadFile(InputStream inputStream, String fileName) throws QiniuException {
        Response response = this.uploadManager.put(inputStream,fileName,getUploadToken(),null,null);
        int retry = 0;
        while (response.needRetry() && retry < 3)
        {
            response = this.uploadManager.put(inputStream,fileName,getUploadToken(),null,null);
            retry++;
        }
        if(response.statusCode == 200)
        {
            //返回 图片连接
            return new StringBuffer().append("http://blog.domain.com/").append(fileName).toString();
        }
        return "上传失败！";
    }

    /**
     * 删除七流云上的相关文件
     * @param key
     * @return
     * @throws QiniuException
     */
    @Override
    public String delete(String key) throws QiniuException {
        Response response = bucketManager.delete(this.bucket,key);
        int retry = 0;
        while (response.needRetry() && retry < 3)
        {
            response = bucketManager.delete(bucket,key);
        }
        return response.statusCode == 200?"删除成功！":"删除失败！";
    }

    @Override
    public void afterPropertiesSet() {
        this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
    }

    /**
     * 获取上传凭证
     * @return
     */
    @Override
    public String getUploadToken() {
        return this.auth.uploadToken(bucket,null,3600,putPolicy);
    }
}
